package com.jarvis.pzz.modules.main;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.PopupMenu;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jarvis.pzz.MainActivity;
import com.jarvis.pzz.R;
import com.jarvis.pzz.VideoViewActivity;
import com.jarvis.pzz.common.AppStatus;
import com.jarvis.pzz.common.BaseFragment;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.AdvPlateModel;
import com.jarvis.pzz.models.AdvertisesmetListModel;
import com.jarvis.pzz.models.AreaListModel;
import com.jarvis.pzz.models.CityModel;
import com.jarvis.pzz.models.LabelListModel;
import com.jarvis.pzz.models.MainIndexModel;
import com.jarvis.pzz.models.MeasureAreaListModel;
import com.jarvis.pzz.models.PriceAreaListModel;
import com.jarvis.pzz.models.SearchTypeModel;
import com.jarvis.pzz.models.ShopListModel;
import com.jarvis.pzz.modules.findshop.FindShopActivity;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.main.adapter.MpAdapter;
import com.jarvis.pzz.modules.sellshop.SellShopActivity;
import com.jarvis.pzz.modules.shop.view.MapFindNewActivity;
import com.jarvis.pzz.modules.shop.view.ShopDetailsActivity;
import com.jarvis.pzz.modules.main.adapter.ZpAdapter;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.DipUtil;
import com.jarvis.pzz.util.DisplayUtil;
import com.jarvis.pzz.util.IntentUtil;
import com.jarvis.pzz.util.SharePreferenceKey;
import com.jarvis.pzz.util.ViewHolderUtil;
import com.jarvis.pzz.util.net.RequestApi;
import com.jarvis.pzz.widget.BannerHeaderView;
import com.jarvis.pzz.widget.MyScrollView;
import com.jarvis.pzz.widget.ScrollGridView;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.functions.Action1;

/**
 * Created by jarvis.liu on 2016-11-02.
 */

public class MainFragment extends BaseFragment implements View.OnClickListener, View.OnKeyListener {
    @BindView(R.id.scroll)
    MyScrollView scroll;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.rel_bg)
    RelativeLayout rel_bg;
    @BindView(R.id.rel_middle)
    RelativeLayout rel_middle;
    @BindView(R.id.banner)
    BannerHeaderView banner;
    @BindView(R.id.ed_search)
    EditText ed_search;
    @BindView(R.id.tv_left)
    TextView tv_left;
    @BindView(R.id.lin)
    LinearLayout lin;
    @BindView(R.id.lin_list)
    LinearLayout lin_list;
    @BindView(R.id.rel_find_shop_1)
    RelativeLayout rel_find_shop_1;
    @BindView(R.id.img_adv)
    ImageView img_adv;
    @BindView(R.id.img_more)
    ImageView img_more;
    @BindView(R.id.img_search)
    ImageView img_search;
    @BindView(R.id.view)
    View view;


    @BindView(R.id.sg_zp)
    ScrollGridView sg_zp;
    @BindView(R.id.sg_mp)
    ScrollGridView sg_mp;

    @BindView(R.id.tv_wyzp_1)
    TextView tv_wyzp_1;
    @BindView(R.id.lin_wyzp_1)
    LinearLayout lin_wyzp_1;
    @BindView(R.id.tv_wyzp_2)
    TextView tv_wyzp_2;
    @BindView(R.id.lin_wyzp_2)
    LinearLayout lin_wyzp_2;
    @BindView(R.id.tv_wymp_3)
    TextView tv_wymp_3;
    @BindView(R.id.lin_wymp_3)
    LinearLayout lin_wymp_3;
    @BindView(R.id.tv_wymp_4)
    TextView tv_wymp_4;
    @BindView(R.id.lin_wymp_4)
    LinearLayout lin_wymp_4;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;

    @BindView(R.id.lin_ckxp)
    LinearLayout lin_ckxp;
    @BindView(R.id.lin_bwzp)
    LinearLayout lin_bwzp;
    @BindView(R.id.lin_wtzp)
    LinearLayout lin_wtzp;
    @BindView(R.id.lin_dtzp)
    LinearLayout lin_dtzp;

    @BindView(R.id.tv_shops_count_today)
    TextView tv_shops_count_today;
    @BindView(R.id.tv_shops_count_total)
    TextView tv_shops_count_total;
    @BindView(R.id.tv_search_shops_people_count)
    TextView tv_search_shops_people_count;

    @BindView(R.id.rel_bwzp)
    RelativeLayout rel_bwzp;
    @BindView(R.id.lin_zzzp)
    LinearLayout lin_zzzp;
    @BindView(R.id.lin_zxkf)
    LinearLayout lin_zxkf;
    @BindView(R.id.lin_zp_qz)
    LinearLayout lin_zp_qz;
    @BindView(R.id.lin_vip)
    LinearLayout lin_vip;
    @BindView(R.id.lin_cssp)
    LinearLayout lin_cssp;
    @BindView(R.id.lin_zxgw)
    LinearLayout lin_zxgw;
    @BindView(R.id.lin_60bz)
    LinearLayout lin_60bz;
    @BindView(R.id.lin_mfzp)
    LinearLayout lin_mfzp;
    @BindView(R.id.lin_mp_qg)
    LinearLayout lin_mp_qg;
    private boolean isLoading = false;

    ZpAdapter zpAdapter;
    MpAdapter mpAdapter;

    private List<SearchTypeModel> zp_list = new ArrayList<>();
    private List<SearchTypeModel> zp_list_1 = new ArrayList<>();
    private List<SearchTypeModel> zp_list_2 = new ArrayList<>();
    private List<SearchTypeModel> zp_list_3 = new ArrayList<>();

    private List<SearchTypeModel> mp_list = new ArrayList<>();
    private List<SearchTypeModel> mp_list_1 = new ArrayList<>();
    private List<SearchTypeModel> mp_list_2 = new ArrayList<>();
    private List<SearchTypeModel> mp_list_3 = new ArrayList<>();


    private List<AdvertisesmetListModel> advList = new ArrayList<>();

    private RequestService service;
    private MainIndexModel mainIndexModel;
    private int status = 0;

    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_main;
    }

    @Override
    public void initData() {
        showDialog("loading");
        Call<ResponseData<MainIndexModel>> index = service.index();
        index.enqueue(new Callback<ResponseData<MainIndexModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MainIndexModel>> call, Response<ResponseData<MainIndexModel>> response) {
                isLoading = false;
                System.out.println("2222222222");
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        mainIndexModel = response.body().getData();
                        if (mainIndexModel != null) {
                            setData();
                        }
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
                System.out.println("33333333333");
                disMiss();
            }

            @Override
            public void onFailure(Call<ResponseData<MainIndexModel>> call, Throwable t) {
                disMiss();
                isLoading = false;
                showTextToast(RequestApi.detailError(t));
            }
        });

    }

    private void setData() {
        ;
        AppStatus.setCityList(mainIndexModel.getCityList());
        AppStatus.getInstance().cityName=mainIndexModel.getCityList().get(0).getCityName();
        tv_left.setText(mainIndexModel.getCityList().get(0).getCityName());
        Glide.with(getActivity()).load(mainIndexModel.getAdvPlateList().get(0).getAdvImgPath()).into(img_adv);
        tv_shops_count_today.setText(mainIndexModel.getShopsCountToday());
        tv_shops_count_total.setText(mainIndexModel.getShopsCountTotal());
        tv_search_shops_people_count.setText(mainIndexModel.getSearchShopsPeopleCount());
        advList.clear();
        advList = mainIndexModel.getAdvertisesmetList();
        banner.setImgUrlData(advList);
        setZpValue();
        lin_list.removeAllViews();
//        shopList.clear();
//        shopList.addAll(mainIndexModel.getShopList());
        for (int j = 0; j <mainIndexModel.getShopList().size() ; j++) {
            View convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_shop, null, false);

            final ShopListModel shopListModel = mainIndexModel.getShopList().get(j);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), ShopDetailsActivity.class).putExtra("shopId", shopListModel.getShopId()));
                }
            });
            LinearLayout lin_sign = ViewHolderUtil.get(convertView, R.id.lin_sign);
            ImageView img = ViewHolderUtil.get(convertView, R.id.img);
            ImageView img_top = ViewHolderUtil.get(convertView, R.id.img_top);
            TextView tv_name = ViewHolderUtil.get(convertView, R.id.tv_name);
            TextView tv_demo = ViewHolderUtil.get(convertView, R.id.tv_demo);
            TextView tv_shopIntroduce = ViewHolderUtil.get(convertView, R.id.tv_shopIntroduce);
            LinearLayout lin_bg = ViewHolderUtil.get(convertView, R.id.lin_bg);
            TextView tv_shop_rent = ViewHolderUtil.get(convertView, R.id.tv_shop_rent);

            Glide.with(getActivity()).load(shopListModel.getShopThumbnailImgPath()).placeholder(R.drawable.img_default).error(R.drawable.img_default).into(img);
            tv_name.setText(shopListModel.getShopTitle());
            tv_shop_rent.setText(shopListModel.getShopRent());
            tv_shopIntroduce.setText(shopListModel.getShopIntroduce());
            String sign = shopListModel.getShopLables();
            lin_bg.setBackgroundColor(getActivity().getResources().getColor(R.color.grey_2));
            if(TextUtils.isEmpty(shopListModel.getShopAuditingState())){
                img_top.setVisibility(View.GONE);
            }else {
                if("1".equals(shopListModel.getShopAuditingState())){
                    img_top.setVisibility(View.VISIBLE);
                }else {
                    img_top.setVisibility(View.GONE);
                }
            }
            if (!TextUtils.isEmpty(sign)) {
                String[] split = sign.split(",");
                List<String> list_sign = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    list_sign.add(split[i]);
                }
                lin_sign.removeAllViews();
                int count = list_sign.size();
                if (count > 3) {
                    count = 3;
                }
                for (int i = 0; i < count; i++) {
                    TextView tv2 = new TextView(getActivity());
                    tv2.setText(list_sign.get(i));
                    tv2.setBackgroundColor(getActivity().getResources().getColor(R.color.grey_6));
                    tv2.setTextColor(getActivity().getResources().getColor(R.color.grey_7));
                    tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
                    tv2.setGravity(Gravity.CENTER);
                    tv2.setMaxLines(1);
                    tv2.setEllipsize(TextUtils.TruncateAt.END);
                    tv2.setPadding(3, 3, 3, 3);
                    ViewGroup.LayoutParams layoutParams1 = tv_demo.getLayoutParams();
                    tv2.setLayoutParams(layoutParams1);

                    LinearLayout lin = new LinearLayout(getActivity());
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    if (i != 0) {
                        params.setMargins(10, 0, 0, 0);
                    }
                    lin.setLayoutParams(params);
                    lin.addView(tv2);
                    lin_sign.addView(lin);
                }
            }
            lin_list.addView(convertView);
        }
    }

    private void setMpValue() {
        mp_list_1.clear();
        for (int i = 0; i < mainIndexModel.getAreaList().size(); i++) {
            AreaListModel areaListModel = mainIndexModel.getAreaList().get(i);
            SearchTypeModel model = new SearchTypeModel(areaListModel.getName(), areaListModel.getCode(), "areaId");
            mp_list_1.add(model);
        }
        mp_list_2.clear();
        for (int i = 0; i < mainIndexModel.getMeasureAreaList().size(); i++) {
            MeasureAreaListModel measureAreaListModel = mainIndexModel.getMeasureAreaList().get(i);
            SearchTypeModel model = new SearchTypeModel(measureAreaListModel.getMeasureDisplayName(), measureAreaListModel.getMeasureValue(), "measureArea");
            mp_list_2.add(model);
        }
        mp_list_3.clear();
        for (int i = 0; i < mainIndexModel.getPriceAreaList().size(); i++) {
            PriceAreaListModel priceAreaListModel = mainIndexModel.getPriceAreaList().get(i);
            SearchTypeModel model = new SearchTypeModel(priceAreaListModel.getPriceDisplayName(), priceAreaListModel.getPriceValue(), "priceArea");
            mp_list_3.add(model);
        }
        mp_list_1.clear();
        setMp3to1(mp_list_1);
        setMp3to1(mp_list_2);
        setMp3to1(mp_list_3);
        mpAdapter = new MpAdapter(mp_list, getActivity());
        sg_mp.setAdapter(mpAdapter);
    }

    private void setZpValue() {
        zp_list_1.clear();
        for (int i = 0; i < mainIndexModel.getAreaList().size(); i++) {
            AreaListModel areaListModel = mainIndexModel.getAreaList().get(i);
            SearchTypeModel model = new SearchTypeModel(areaListModel.getName(), areaListModel.getCode(), "areaId");
            zp_list_1.add(model);
        }
        zp_list_2.clear();
        for (int i = 0; i < mainIndexModel.getMeasureAreaList().size(); i++) {
            MeasureAreaListModel measureAreaListModel = mainIndexModel.getMeasureAreaList().get(i);
            SearchTypeModel model = new SearchTypeModel(measureAreaListModel.getMeasureDisplayName(), measureAreaListModel.getMeasureValue(), "measureArea");
            zp_list_2.add(model);
        }
        zp_list_3.clear();
        for (int i = 0; i < mainIndexModel.getLabelList().size(); i++) {
            LabelListModel labelListModel = mainIndexModel.getLabelList().get(i);
            SearchTypeModel model = new SearchTypeModel(labelListModel.getLabName(), labelListModel.getLabId(), "shopLabelId");
            model.setImg_url(labelListModel.getLabImgAbledPath());
            zp_list_3.add(model);
        }
        zp_list.clear();
        setZp3to1(zp_list_1);
        setZp3to1(zp_list_2);
        setZp3to1(zp_list_3);
        zpAdapter = new ZpAdapter(zp_list, getActivity());
        sg_zp.setAdapter(zpAdapter);
    }

    private void setZp3to1(List<SearchTypeModel> list) {
        if (list.size() > 4) {
            for (int i = 0; i < 4; i++) {
                zp_list.add(list.get(i));
            }
        } else if (list.size() == 4) {
            zp_list.addAll(list);
        } else {
            zp_list.addAll(list);
            for (int i = 0; i < 4 - list.size(); i++) {
                SearchTypeModel m = new SearchTypeModel("", "", "");
                zp_list.add(m);
            }
        }
    }

    private void setMp3to1(List<SearchTypeModel> list) {
        if (list.size() > 4) {
            for (int i = 0; i < 4; i++) {
                mp_list.add(list.get(i));
            }
        } else if (list.size() == 4) {
            mp_list.addAll(list);
        } else {
            mp_list.addAll(list);
            for (int i = 0; i < 4 - list.size(); i++) {
                SearchTypeModel m = new SearchTypeModel("", "", "");
                mp_list.add(m);
            }
        }
    }

    @Override
    protected void initView() {
        tv_left.setText(AppStatus.getInstance().cityName);
        ViewGroup.LayoutParams vParams = img_adv.getLayoutParams();
        vParams.height = ((DisplayUtil.getMobileWidth(getActivity()))*350)/1080;
        img_adv.setLayoutParams(vParams);
        service = RequestApi.create(RequestService.class);
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(title.getLayoutParams());
        lp.height = statusBarHeight1 + DipUtil.dip2px(getActivity(), 50);
        title.setLayoutParams(lp);
        title.setPadding(0, statusBarHeight1, 0, 0);

        title.getBackground().setAlpha(0);
        //title.setVisibility(View.GONE);

        // setStatus(true);
        // 实现自定义控件中的监听器
        tv_wyzp_1.setOnClickListener(this);
        tv_wyzp_2.setOnClickListener(this);
        tv_wymp_3.setOnClickListener(this);
        tv_wymp_4.setOnClickListener(this);
        rel_find_shop_1.setOnClickListener(this);
        lin_zzzp.setOnClickListener(this);
        lin_mfzp.setOnClickListener(this);
        lin_60bz.setOnClickListener(this);
        lin_zxkf.setOnClickListener(this);
        lin_vip.setOnClickListener(this);
        lin_zp_qz.setOnClickListener(this);
        lin_cssp.setOnClickListener(this);
        lin_zxgw.setOnClickListener(this);
        rel_bwzp.setOnClickListener(this);
        lin_mp_qg.setOnClickListener(this);
        rel_left.setOnClickListener(this);
        img_adv.setOnClickListener(this);

        lin_ckxp.setOnClickListener(this);
        lin_bwzp.setOnClickListener(this);
        lin_wtzp.setOnClickListener(this);
        lin_dtzp.setOnClickListener(this);
        ed_search.setOnKeyListener(this);

        scroll.setScrollViewListener(new MyScrollView.ScrollViewListener() {

            @Override
            public void onScrollChanged(View scrollView, int x, int y,
                                        int oldx, int oldy) {
                // TODO Auto-generated method stub
                System.out.println("y:" + y + " oldy:" + oldy + " scroll:" + scroll.getScrollY());
                titleAnima(y);
            }

            @Override
            public void onGoOnMove() {
                AppStatus.getInstance().type = "";
                if (MainActivity.Instance != null)
                    MainActivity.Instance.toShop();
            }

            @Override
            public void onReflash() {
                if (!isLoading) {
                    isLoading = true;
                    initData();
                }
            }

        });
        sg_zp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SearchTypeModel searchTypeModel = zp_list.get(position);
                AddOrChangeData(searchTypeModel);
                AppStatus.getInstance().type = "0";
                if (MainActivity.Instance != null)
                    MainActivity.Instance.toShop();
            }
        });
        sg_mp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SearchTypeModel searchTypeModel = mp_list.get(position);
                AddOrChangeData(searchTypeModel);
                AppStatus.getInstance().type = "1";
                if (MainActivity.Instance != null)
                    MainActivity.Instance.toShop();
            }
        });

    }

    /**
     * 出现渐变效果
     */
    public void titleAnima(int y) {
//        if (y == 0) {
//            title.setVisibility(View.GONE);
//            //setStatus(true);
//        } else {
//            title.setVisibility(View.VISIBLE);
//            // setStatus(false);
//        }
        int scrollHeight = banner.getHeight();
        float scrollPercent = (float) y / scrollHeight;
        if(y==0){
            tv_left.setTextColor(getResources().getColor(R.color.white));
            ed_search.setTextColor(getResources().getColor(R.color.white));
            ed_search.setHintTextColor(getResources().getColor(R.color.white));
            rel_bg.setBackground(getResources().getDrawable((R.drawable.img_ed_bg_gray)));
            img_more.setImageDrawable(getResources().getDrawable((R.drawable.img_more_white)));
            img_search.setImageDrawable(getResources().getDrawable((R.drawable.img_search_white)));
        }
        if (y > scrollHeight) {
            scrollPercent = 1;
            tv_left.setTextColor(getResources().getColor(R.color.grey_1));
            ed_search.setTextColor(getResources().getColor(R.color.grey_1));
            ed_search.setHintTextColor(getResources().getColor(R.color.grey_1));
            rel_bg.setBackground(getResources().getDrawable((R.drawable.img_ed_bg_white)));
            img_more.setImageDrawable(getResources().getDrawable((R.drawable.img_more_gray)));
            img_search.setImageDrawable(getResources().getDrawable((R.drawable.img_search_gray)));
        }
        title.getBackground().setAlpha((int) (255 * scrollPercent));

        // 如果有文字的话，还可以设置文字的颜色渐变
        // int color = topText.getTextColors().getDefaultColor();
        // int r = Color.red(color);
        // int g = Color.green(color);
        // int b = Color.blue(color);
        // int changeToColor = Color.argb((int) (255 * (1 - scrollPercent)), r,
        // g, b);
        // topText.setTextColor(changeToColor);
    }

    private void AddOrChangeData(SearchTypeModel searchTypeModel) {
        if ("全部商铺".equals(searchTypeModel.getDisplayValue())) {
            List<SearchTypeModel> list = new ArrayList<>();
            AppStatus.getInstance().setList(list);
        } else {
            boolean is_add = true;
            List<SearchTypeModel> list = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (searchTypeModel.getType().equals(list.get(i).getType())) {
                    is_add = false;
                    list.set(i, searchTypeModel);
                }
            }
            if (is_add) {
                list.add(searchTypeModel);
            }
            AppStatus.getInstance().setList(list);
        }
    }

    private void showData(LinearLayout lin, TextView tv) {
        tv_wyzp_1.setBackgroundResource(R.color.white);
        tv_wyzp_2.setBackgroundResource(R.color.white);
        tv_wymp_3.setBackgroundResource(R.color.white);
        tv_wymp_4.setBackgroundResource(R.color.white);

        lin_wyzp_1.setVisibility(View.GONE);
        lin_wyzp_2.setVisibility(View.GONE);
        lin_wymp_3.setVisibility(View.GONE);
        lin_wymp_4.setVisibility(View.GONE);

        lin.setVisibility(View.VISIBLE);
        tv.setBackgroundResource(R.color.grey_2);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
            tv_left.setText(AppStatus.getInstance().cityName);
            setStatusWhite(false);
            System.out.println("un hidden:" + getClass().toString());
            status = 1;
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
        } else {
            System.out.println("hidden:" + getClass().toString());
            status = 2;
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        tv_left.setText(AppStatus.getInstance().cityName);
        setStatusWhite(false);
        if (status == 0) {
            System.out.println("onResume:" + getClass().toString());
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        if (status == 0) {
            System.out.println("onPause:" + getClass().toString());
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.img_adv:
                AdvPlateModel advPlateModel = mainIndexModel.getAdvPlateList().get(0);
                Intent intents=new Intent(getActivity(), VideoViewActivity.class);
                intents.putExtra("type","0");
                intents.putExtra("url",advPlateModel.getAdvLink());
                intents.putExtra("title",advPlateModel.getAdvTitle());
                startActivity(intents);
                break;
            case  R.id.rel_left:
                List<CityModel> cityList = mainIndexModel.getCityList();

                final PopupMenu popupMenu = new PopupMenu(getActivity(), v);
                for (int i = 0; i < cityList.size(); i++) {
                    popupMenu.getMenu().add(0, i, 0, cityList.get(i).getCityName()).setTitleCondensed(String.valueOf(cityList.get(i).getCityId()));
                }
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        String productName = item.getTitle().toString();
                        tv_left.setText(productName);
                        AppStatus.getInstance().cityName=productName;
                        return true;
                    }
                });
                popupMenu.show();

                break;
            case  R.id.lin_ckxp:
                AppStatus.getInstance().type = "";
                if (MainActivity.Instance != null)
                    MainActivity.Instance.toShop();
                break;
            case  R.id.lin_bwzp:
                //找铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 2;
                    startActivity(new Intent(getActivity(), FindShopActivity.class));
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case  R.id.lin_wtzp:
                //转铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 3;
                    Intent intent = new Intent(getActivity(), SellShopActivity.class);
                    intent.putExtra("TO_SELL", 0);
                    startActivity(intent);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case  R.id.lin_dtzp:
                startActivity(MapFindNewActivity.class);
                break;
            case R.id.lin_zxkf:
            case R.id.lin_zxgw:
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext())
                        .setTitle("温馨提示")
                        .setMessage("400-928-2858")
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(getActivity())
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(getActivity(), "400-928-2858");
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                dialogBuilder.create().show();
                break;
            case R.id.lin_zp_qz:
                startActivity(new Intent(getActivity(), ReleaseListActivity.class).putExtra("type", "2"));
                break;
            case R.id.lin_60bz:
            case R.id.lin_mfzp:
                //转铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 3;
                    Intent intent = new Intent(getActivity(), SellShopActivity.class);
                    intent.putExtra("TO_SELL", 0);
                    startActivity(intent);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.rel_bwzp:
                //找铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 2;
                    startActivity(new Intent(getActivity(), FindShopActivity.class));
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.lin_vip:
            case R.id.lin_cssp:
            case R.id.lin_zzzp:
                //卖铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 5;
                    Intent intent2 = new Intent(getActivity(), SellShopActivity.class);
                    intent2.putExtra("TO_SELL", 1);
                    startActivity(intent2);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.lin_mp_qg:
                startActivity(new Intent(getActivity(), ReleaseListActivity.class).putExtra("type", "3"));
                break;
            case R.id.rel_find_shop_1:
                AppStatus.getInstance().type = "";
                if (MainActivity.Instance != null)
                    MainActivity.Instance.toShop();
                break;
            case R.id.tv_wyzp_1:
                showData(lin_wyzp_1, tv_wyzp_1);
                break;
            case R.id.tv_wyzp_2:
                showData(lin_wyzp_2, tv_wyzp_2);
                break;
            case R.id.tv_wymp_3:
                if (mp_list.size() == 0) {
                    setMpValue();
                }
                showData(lin_wymp_3, tv_wymp_3);
                break;
            case R.id.tv_wymp_4:
                showData(lin_wymp_4, tv_wymp_4);
                break;

        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            // 先隐藏键盘
            ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getActivity().getCurrentFocus()
                            .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            AppStatus.getInstance().type = "";
            AppStatus.getInstance().value=ed_search.getText().toString();
            AppStatus.getInstance().setList(new ArrayList<SearchTypeModel>());
            if (MainActivity.Instance != null)
                MainActivity.Instance.toShop();
            ed_search.setText("");
            return true;
        } else {
            return false;
        }
    }
}
